package com.zzyl.job;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.zzyl.constant.CacheConstants;
import com.zzyl.entity.AlertRule;
import com.zzyl.entity.Device;
import com.zzyl.entity.DeviceData;
import com.zzyl.mapper.AlertRuleMapper;
import com.zzyl.mapper.DeviceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @author Administrator
 */
@Slf4j
@Component
public class MyTask {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private AlertRuleMapper alertRuleMapper;

    //    @Scheduled(fixedRate = 2000) //固定速率，每隔2秒执行一次方法
    @Scheduled(cron = "0/20 * * * * ? ")
    public void task() {
    //     if(Boolean.FALSE.equals(redisTemplate.hasKey("UpdateTime"))){
    //         redisTemplate.opsForValue().set("UpdateTime",LocalDateTimeUtil.formatNormal(LocalDateTime.now()));
    //     }
    //     if(Boolean.FALSE.equals(redisTemplate.hasKey("HeartRateLowCount"))){
    //         redisTemplate.opsForValue().set("HeartRateLowCount", String.valueOf(0));
    //     }
    //     List<AlertRule> alertRules = alertRuleMapper.getAll();
    //     if (ObjectUtil.isEmpty(alertRules)) {
    //         return;
    //     }
    //     AlertRule alertRule = alertRules.get(0);
    //     List<Device> devices = deviceMapper.selectByIotIdAndProductKey(alertRule.getProductKey());
    //     Device device = devices.get(0);
    //     Object o = redisTemplate.opsForHash().get(CacheConstants.DEVICE_LAST_DATA_CACHE_KEY, device.getIotId());
    //     List<DeviceData> dataList = null;
    //     if (ObjectUtil.isNotEmpty(o)) {
    //         dataList = JSONUtil.toList(JSONUtil.toJsonStr(o), DeviceData.class);
    //     }
    //
    //     if (dataList != null) {
    //         for (DeviceData data : dataList) {
    //             if (data.getFunctionId().equals(alertRule.getFunctionId())) {
    //                 String timeStr = redisTemplate.opsForValue().get("UpdateTime");
    //                 if (timeStr != null) {
    //                     DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //                     LocalDateTime storedTime = LocalDateTime.parse(timeStr, formatter);
    //                     if (LocalDateTimeUtil.between(storedTime, data.getUpdateTime()).toSeconds() > 10) {
    //                         if (Integer.parseInt(data.getDataValue()) < 65) {
    //                             redisTemplate.opsForValue().set("UpdateTime",LocalDateTimeUtil.formatNormal(data.getUpdateTime()));
    //                             redisTemplate.opsForValue().set("HeartRateLowCount", String.valueOf((Integer.parseInt(redisTemplate.opsForValue().get("HR"))+1)));
    //                         }
    //                     }
    //                 }
    //             }
    //         }
    //     }
    //     String count = redisTemplate.opsForValue().get("HeartRateLowCount");
    //     if(count!=null&&Integer.parseInt(count)>=3){
    //         redisTemplate.opsForValue().set("HeartRateLowCount", String.valueOf(0));
    //         log.warn("3 times!!!");
    //     }
    }
}
